package com.zj.syzs.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zj.shouyou.R;

import java.util.ArrayList;
import java.util.List;


public class GuideActivity extends Activity {
    private ViewPager viewPager;
    private List<ImageView> imageViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initView();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewpager_guide);
        imageViews = new ArrayList<>();

        ImageView imgView = null;
        for (int i = 1; i <= 3; i++) {
            imgView = new ImageView(getApplicationContext());
            imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imgView.setImageResource(getResources().getIdentifier("bg_guide_0" + i, "mipmap", getPackageName()));
            imageViews.add(imgView);

            if (i == 3) {
                imgView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO 启动主页面
                        finish(); // TODO 关闭当前页面
                        startActivity(new Intent(getApplicationContext(), MainActivity.class));
                    }
                });
            }
        }

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return imageViews.size();
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(imageViews.get(position));
                return imageViews.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                container.removeView(imageViews.get(position));
            }
        });
    }
}

